package com.hec.yygh.user.controller;


import com.hec.yygh.common.result.R;
import com.hec.yygh.common.utils.AuthContextHolder;
import com.hec.yygh.model.user.Patient;
import com.hec.yygh.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.nio.cs.US_ASCII;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 就诊人表 前端控制器
 * </p>
 *
 * @author hec
 * @since 2022-11-24
 */
@RestController
@RequestMapping("/api/userinfo/patient")
public class PatientController {

    @Autowired
    private PatientService patientService;

    //添加就诊人
    @PostMapping("auth/save")
    public R savePatient(@RequestBody Patient patient, HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        patientService.savePatient(patient,userId);
        return R.ok();
    }

    //删除就诊人
    @DeleteMapping("/auth/remove/{id}")
    public R removeById(@PathVariable("id")Integer id){
        patientService.removeById(id);
        return R.ok();
    }

    //修改页面需先回显就诊人信息
    @GetMapping("/auth/get/{id}")
    public R getInfoById(@PathVariable("id")Integer id){
        Patient patient = patientService.getInfoById(id);
        return R.ok().data("patient",patient);
    }

    //修改就诊人
    @PutMapping("/auth/update")
    public R updateById(@RequestBody Patient patient){
        patientService.updateById(patient);
        return R.ok();
    }

    //查询就诊人列表
    @GetMapping("/auth/findAll")
    public R findAll(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> patientList= patientService.getPatientByUserId(userId);
        return R.ok().data("list",patientList);
    }

}

